import numpy as np
import csv
import random
from datetime import datetime
from time import strftime
from dbfpy import dbf

'''
read dbf file
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    i = 0
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        sKey.append(ra.fieldnames[3])
        sKey.append(ra.fieldnames[-2])
        sKey.append(ra.fieldnames[-1])
        print i
        i += 1
    sKey = np.array(sKey)
    sKey.shape=(-1,3)
    return sKey

def build_data(inputFile):
    sKey = []
    reader = csv.reader(open(inputFile, 'rb'), delimiter='	')
    i = 0
    for record in reader:
        #print record[0]
        if record[0] in outCon:
            continue
        for item in record:
            sKey.append(item)
        i += 1
    sKey = np.array(sKey)
    sKey.shape=(i, -1)
    return sKey


#--------------------------------------------------------------------------
#MAIN
if __name__ == "__main__":
    print "begin at " + getCurTime()
    #filePath = 'C:/_DATA/US/Counties/'
    filePath = 'C:/_DATA/migration89_08/COUNTY Migration/SaTScan/'
    #inputDBF = filePath + 'tl_2010_us_county10_Project.dbf'
    outCon = ['02013', '02016', '02020', '02050', '02060', '02068', '02070', '02090',
              '02100', '02105', '02110', '02122', '02130', '02150', '02164', '02170',
              '02180', '02185', '02188', '02220', '02230', '02240', '02261', '02270',
              '02282', '02290', '15001', '15003', '15007', '15009']
    inputCSV = filePath + 'spacetime_total_in_case.cas'
    inputCSV = filePath + 'spacetime_total_in_pop.pop'
    data = build_data(inputCSV)


    fileLoc = 'C:/_DATA/migration89_08/COUNTY Migration/SaTScan/spacetime_total_in_pop_con.cas'
    np.savetxt(fileLoc, data, delimiter=' ', fmt = '%s')

    print "end at " + getCurTime()
    print "========================================================================"  
    
            